class Lxy_params_mgr {
	// _input_params = {};   // 输入参数(Input)
	// _params = {};        // 定义参数(如：组件绑定参数，应答消息保存到指定的参数中)
	constructor(_params, _input_params, _global_params = {}) {
		this._params = _params || null;
		this._input_params = _input_params || null;
		this._global_params = _global_params || null;
		console.log('Lxy_params_mgr_params', _params, _input_params, _global_params, this._global_params);
	}

	getParam(key, ifArray, ArrayBind) {
		if (!key) {
			return null;
		}
		// xx.yy.mm
		const keys = key.split('.');
		// console.log('keyskeyskeyskeys', keys, keys[0]);
		if (keys.length <= 0 || keys[0] === '') {
			console.log('[Error][getParam]Get param fail, key is empty.');
			return null;
		}

		let i = 0;
		let tmp = this._params;
		let returnTmp = {};
		if (keys[0] === 'In') {
			tmp = this._input_params;
			i = 1;
		}
		if (keys[0] === 'G') {
			tmp = this._global_params;
			i = 1;
		}
		// console.log('tttttttttttttt', tmp);
		if (tmp === null) {
			console.error('[Error][getParam]Get param fail, the data src is null, key: ', key);
			return;
		}

		let varName = '';
		let index = '';
		let isGetArrayinObj = false;
		let tempObj = false;
		for (; i < keys.length; i++) {
			if (keys[i] === '') {
				console.error('[Error][getParam]Get param fail, key is empty, index: ', i, key);
				return null;
			}

			const begin = keys[i].indexOf('[');
			const end = keys[i].indexOf(']');
			if (begin < 0) {
				if (typeof tmp !== 'object') {
					console.error('[Error][getParam]Get param fail, parent is not object, key: ', keys[i], tmp);
					return null;
				}
				if (ifArray) {
					returnTmp = [];
					tmp[ArrayBind].forEach((item) => {
						returnTmp.push(item[keys[i]]);
					});
				} else {
					try {
						if (![undefined, null, NaN].includes(tmp[keys[i]]) && !isGetArrayinObj && !tempObj) {
							returnTmp = tmp[keys[i]];
							tempObj = true;
						} else if (![undefined, null, NaN].includes(returnTmp[keys[i]])) {
							returnTmp = returnTmp[keys[i]];
						} else {
							returnTmp = '';
						}
					} catch (error) {
						console.error('[Error][getParam]Get param fail，key is not exist,key:', key, '  all Params:', tmp, '---', keys[i]);
						return null;
					}
					// returnTmp = tmp[keys[i]];
				}
			} else if (begin > 0 && end > begin) {
				isGetArrayinObj = true;
				varName = keys[i].substring(0, begin);
				index = keys[i].substring(begin + 1, end);
				const iIndex = Number.parseInt(index);
				console.log('beginbeginbeginbegin', varName, index, iIndex);
				if (!tmp.hasOwnProperty(varName)) {
					console.error('[Error][getParam]Get param fail, key is not exist, key: ', varName, key, tmp);
					return null;
				}
				if (!Array.isArray(tmp[varName])) {
					console.error('[Error][getParam]The data not array, key: ', varName, tmp, key);
					return null;
				}
				returnTmp = tmp[varName][iIndex];
				console.log('returnTmpreturnTmp', returnTmp);
			} else {
				console.error('[Error][getParam]Get param failGet param fail, key is invalid, key: ', keys[i], key);
				return null;
			}
			if (returnTmp === undefined || returnTmp === null) {
				console.error('[Error][getParam]Get param fail, key index: ', i, ', key: ', keys[i], key);
				return null;
			}
		}
		return returnTmp;
	}
	getParamAsNumber(key) {
		const ret = this.getParam(key);
		if (typeof ret === 'number') {
			return ret;
		}

		if (typeof ret === 'string') {
			parseFloat(ret);
		}

		return null;
	}
}

export default Lxy_params_mgr;
